package com.xunyin.modules.store.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.exception.BadRequestException;
import com.xunyin.modules.log.Log;
import com.xunyin.modules.aop.ForbidSubmit;
import com.xunyin.modules.store.domain.TStore;
import com.xunyin.modules.store.param.TStoreQueryParam;
import com.xunyin.modules.store.service.TStoreService;
import com.xunyin.modules.store.service.dto.TStoreQueryCriteria;
import com.xunyin.utils.location.GetTencentLocationVO;
import com.xunyin.utils.location.LocationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author xunyinyunshang  <3594419785@qq.com>
 * @date 2022-09-16
 */
@Api(tags = "共享空间:门店管理")
@RestController
@RequestMapping("api")
public class StoreController {
    private final IGenerator generator;
    private final TStoreService tStoreService;

    public StoreController(TStoreService tStoreService, IGenerator generator) {
        this.tStoreService = tStoreService;
        this.generator = generator;
    }

    @Log("查询门店")
    @ApiOperation(value = "查询门店")
    @GetMapping(value = "/tStore")
    @PreAuthorize("hasAnyRole('admin','MAINPART_ALL','MAINPART_SELECT')")
    public ResponseEntity getTStore(TStoreQueryCriteria criteria, Pageable pageable) {
        Map<String, Object> queryAll = tStoreService.queryAll(criteria, pageable);
        return new ResponseEntity<>(queryAll, HttpStatus.OK);
    }

    @ForbidSubmit
    @Log("新增门店")
    @ApiOperation(value = "新增门店")
    @PostMapping(value = "/tStore")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TStore resources) {
        resources.setStatus(1);
        return new ResponseEntity<>(tStoreService.save(resources), HttpStatus.CREATED);
    }

    @ForbidSubmit
    @Log("修改门店")
    @ApiOperation(value = "修改门店")
    @PutMapping(value = "/tStore")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TStore resources) {
        tStoreService.saveOrUpdate(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @ForbidSubmit
    @Log("删除")
    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/tStore/{id}")
    @PreAuthorize("hasAnyRole('admin','YXSYSTEMUSERLEVEL_ALL','YXSYSTEMUSERLEVEL_DELETE')")
    public ResponseEntity delete(@PathVariable Integer id) {
        tStoreService.removeById(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("获取门店列表")
    @ApiOperation("获取门店列表")
    @PostMapping(value = "/listStore")
    // @PreAuthorize("@el.check('yxSystemStore:list')")
    public ResponseEntity<Object> listStore(@Validated @RequestBody TStoreQueryParam queryParam) {

        return new ResponseEntity<>(tStoreService.listStore(queryParam), HttpStatus.OK);

    }

    @PostMapping(value = "/getL")
    @Log("获取经纬度")
    @ApiOperation("获取经纬度")
    @PreAuthorize("@el.check('yxSystemStore:getl')")
    public ResponseEntity<GetTencentLocationVO> create(@Validated @RequestBody String jsonStr) {
        JSONObject jsonObject = JSON.parseObject(jsonStr);
        String addr = jsonObject.getString("addr");
        GetTencentLocationVO locationVO = LocationUtils.getLocation(addr);
        if (locationVO.getStatus() != 0) {
            throw new BadRequestException(locationVO.getMessage());
        }
        return new ResponseEntity<>(locationVO, HttpStatus.CREATED);
    }
}
